import { postLoginUserLogin, postLoginGetLoginUserInfo } from "@/api/user";
const useUserStore = defineStore("user", {
  state: () => ({}),

  getters: {
    userInfo: (state) => {
      return { ...state };
    },
  },

  actions: {
    setInfo(info) {
      this.$patch(info);
    },

    resetInfo() {
      this.$reset();
    },

    async info() {
      const res = await postLoginGetLoginUserInfo();
      this.setInfo(res.data);
    },

    async logout() {
      this.logoutCallBack();
    },

    logoutCallBack() {
      this.resetInfo();
      window.localStorage.clear()
    },

    async loginByUserName(loginForm) {
      try {
        const res = await postLoginUserLogin(loginForm)
        useStorage("userInfo", res.data);
        useStorage("token", res.data.token);
      } catch (error) {
        window.localStorage.clear()
        throw error;
      }
    }
  }
});

export default useUserStore